﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shell;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ScarecrowLibrary.Network;
using System.Collections.ObjectModel;

using ScarecrowLibrary.IO;

namespace Scarecrow_Network_Manager
{
    /// <summary>
    /// Logica di interazione per ProfileControl.xaml
    /// </summary>
    public partial class ProfileControl : UserControl
    {
        #region Properties
        public ObservableCollection<ProfileData> ProfileWatcher
        { get { return this._profileWatcher; } }
        public scNetworkProfile Profile
        {
            get;
            private set;
        }
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                isActive = value;
                if (isActive)
                {
                    this.txtTitle.Text += Environment.NewLine + string.Format("({0})", Properties.Resources.ActiveProfile);
                    this.txtTitle.Background = Brushes.Green;
                }
                else
                {
                    this.txtTitle.Text = this.Profile.Name;
                    this.txtTitle.Background = Brushes.Transparent;
                }
            }
        }
        #endregion

        #region Fields
        ObservableCollection<ProfileData> _profileWatcher = new ObservableCollection<ProfileData>();
        bool isActive;
        Point lastPressed;
        #endregion

        public ProfileControl(scNetworkProfile np)
        {
            try
            {
                isActive = false;
                Profile = np;
                this.Name = "N" + _profileWatcher.Count;
                scAdapterProfile[] profiles = np.GetProfiles();
                profiles = profiles.Where((p) => p.Enabled).ToArray();
                foreach (var profile in profiles)
                {
                    scAdapterProfile ap = profile;
                    this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.Adapter, Value = ap.NetworkAdapter });
                    if (ap.IsIPDhcp)
                    {
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.DHCPIP, Value = Properties.Resources.Yes });
                    }
                    else
                    {
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.IPAddress, Value = ap.IPAddress });
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.SubnetMask, Value = ap.SubnetMask });
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.Gateway, Value = ap.Gateway });
                    }

                    if (ap.IsDNSDhcp)
                    {
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.DHCPDNS, Value = Properties.Resources.Yes });
                    }
                    else
                    {
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.DNS1, Value = ap.DNS1 });
                        this._profileWatcher.Add(new ProfileData { Field = Properties.Resources.DNS2, Value = ap.DNS2 });
                    }
                }

                InitializeComponent();

                this.txtTitle.Text = np.Name;
                this.imgImage.Source = Global.LoadImageFromPath(np.ImagePath).Source;
            }
            catch(Exception exc)
            {
                Global.NotifyCrash(exc);
            }
        }

        private void imgBookmark_MouseDown(object sender, MouseButtonEventArgs e)
        {
            lastPressed = e.GetPosition(this.imgBookmark);
        }

        private void imgBookmark_MouseUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (lastPressed == e.GetPosition(this.imgBookmark) && this.imgBookmark.Tag != null)
                {
                    if (this.imgBookmark.Tag.ToString() == "active")
                    {
                        this.imgBookmark.Tag = "!active";
                        if (Properties.Settings.Default.Bookmarks.Contains(this.Profile.Name))
                            Properties.Settings.Default.Bookmarks.Remove(this.Profile.Name);
                    }
                    else
                    {
                        this.imgBookmark.Tag = "active";
                        if (Properties.Settings.Default.Bookmarks.Count <= 4)
                        {
                            MainWindow mw = Application.Current.Windows[0] as MainWindow;
                            ThumbButtonInfo tbi = new ThumbButtonInfo();
                            tbi.Description = Profile.Name;
                            tbi.ImageSource = Global.LoadImageFromPath(Profile.ImagePath).Source;
                            tbi.Click += new EventHandler(mw.tbi_Click);
                            mw.TaskbarItemInfo.ThumbButtonInfos.Insert(0, tbi);
                            Properties.Settings.Default.Bookmarks.Add(this.Profile.Name);
                            Properties.Settings.Default.Save();
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Global.NotifyCrash(exc);
            }
        }
    }

    public class ProfileData
    {
        public string Field { get; set; }
        public string Value { get; set; }
    }
}
